package com.gen.info.generate_cluster_information.service;

import com.gen.info.generate_cluster_information.config.SourceDataSource;
import com.gen.info.generate_cluster_information.mode.ConnectionInfo;
import com.gen.info.generate_cluster_information.mode.OceanBaseClusterInfoDO;
import com.gen.info.generate_cluster_information.mode.RSPoolVO;
import com.gen.info.generate_cluster_information.mode.RSUnitVO;
import com.gen.info.generate_cluster_information.util.CommonUtil;
import com.gen.info.generate_cluster_information.util.DBUtil;
import com.gen.info.generate_cluster_information.util.OceanBaseSqlObjectGenerateUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.*;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
@Service
@Slf4j
public class CreateResourcePool {
    @Autowired
    private SourceDataSource sourceDataSource;
    public  void createResourcePool(HashMap<String, RSPoolVO> rsPoolVOHashMap){
        //构建连接信息
        ConnectionInfo connectionInfo = new ConnectionInfo();
        BeanUtils.copyProperties(sourceDataSource,connectionInfo);
        log.info("连接信息构建完成："+connectionInfo.toString());
     rsPoolVOHashMap.values().forEach(v ->{
         try {
             final Connection connetcion = DBUtil.getConnetcion(connectionInfo);
             final PreparedStatement smt = DBUtil.getPreparedStatement(connetcion,"select * from oceanbase.__all_resource_pool where name = ?");
             smt.setString(1,v.getPoolName());
             final ResultSet rs = smt.executeQuery();
             //判断资源池是否已经存在
             if (!rs.next()) {
                 final Statement stmt = DBUtil.getCreateStatement(connetcion);
                 final String rsPoolSql = OceanBaseSqlObjectGenerateUtil.getRSPoolSql(v);
                 stmt.executeUpdate(rsPoolSql);
                     log.info("资源池" + v.getPoolName() + "建立成功！！");
             }else {
                 log.info("资源池"+v.getPoolName()+"已经被创建！！");
             }
         } catch (SQLException throwables) {
             throwables.printStackTrace();
         }finally {
             DBUtil.closeConnection();
         }
     });
    }
    public  HashMap<String, RSPoolVO> getRSPool(Collection<OceanBaseClusterInfoDO> oceanBaseClusterInfoDOS, HashMap<String, RSUnitVO> rsUnitVOS){
        final HashMap<String, RSPoolVO> rsPoolVOS = new HashMap<>();
        int counter = 1;
        for (OceanBaseClusterInfoDO oceanBaseClusterInfoDO : oceanBaseClusterInfoDOS) {
            final RSPoolVO rsPoolVO = new RSPoolVO();
            rsPoolVO.setPoolName(oceanBaseClusterInfoDO.getTenant()+"_0"+counter);
            rsPoolVO.setRsUnitVO(rsUnitVOS.get(oceanBaseClusterInfoDO.getTenant()));
            rsPoolVO.setUnitNum(CommonUtil.getUnitNum(oceanBaseClusterInfoDO.getMode()));
            rsPoolVO.setZonelist(CommonUtil.getZoneList(oceanBaseClusterInfoDO.getMode()));
            rsPoolVOS.put(oceanBaseClusterInfoDO.getTenant(),rsPoolVO);
            counter++;
        }
       return rsPoolVOS;
    }
}
